<?php
/**
 * CLAROLINE
 *
 * e-portfolio tool for Claroline
 *
 * @author Pierre Raynaud <pierre.raynaud@u-clermont1.fr>
 *
 * @package PORTFOL
 *
 */


$tlabelReq = 'PORTFOL';
$msgList   = array();
$gidReset  = true;

if ( isset($_REQUEST['cmd']) ) $cmd = $_REQUEST['cmd'];
else                           $cmd = null;

/**
 *  CLAROLINE MAIN SETTINGS
 */

require_once 'lib/portfolio.lib.php';
require_once 'lib/classes/modele.class.php';
require_once get_path('incRepositorySys').'/lib/user.lib.php';

$afficherStats = true;

// Gestion de l'objet modele
$modele = new Modele();
$modele->setCourseCode('TEST');
$modele->setCreateur($_uid);


$is_allowedToEdit  = true;

$nameTools = "Portfolio";

$QUERY_STRING=''; // used for the breadcrumb 
                  // when one need to add a parameter after the filename

if (isset($is_allowedToEdit))
{
	// Création d'un nouveau modele
	if ($cmd == 'exCreerModele')
	{
		if (isset($_REQUEST['nom']))
		{		
			if ($modele->setNom($_REQUEST['nom']))
			{
				$cmd = 'editerModele';

				if ($_REQUEST['heritage'] != -1)
				{
					$modele->setHeritage($_REQUEST['heritage']);
					$modele->save();
					generationFiche($modele->getModeleId(),$_REQUEST['heritage']);
				}	
			}
			else
			{
				$dialogBox .= get_lang('Unable to create template');
			}
		}
		else
		{
			$dialogBox .= get_lang('Template title required');
			$cmd = 'rqCreerModele';
		}	
	}

	// Formulaire de création
	if ($cmd == 'rqCreerModele')
	{
		$dialogBox .= "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">"
			."<label for=\"comment\">".get_lang('New template title')."</label>"
			."<input type=\"hidden\" name=\"cmd\" value=\"exCreerModele\">"
			."<br><input type=\"text\" name=\"nom\">"
			."<br><label for=\"comment\">".get_lang('Copy from')."</label>"
			."<br><select name=\"heritage\">"
			."<option value=\"-1\">Aucun</option>";

		foreach ($modele->chercherListeModele() as $modele_id)
		{
			$modeleExistant = new Modele();
			$modeleExistant -> load($modele_id['fiche_id']);
			$dialogBox .= "<option value=\"".$modeleExistant->getModeleId()."\">".$modeleExistant->getNom()."</option>";
		}

		$dialogBox .= "</select><br><br><input style=\"font-weight: bold\" type=\"submit\" value=\"".get_lang('OK')."\">";
	}
	
	// Redirection edition d'un modele
	if ($cmd == 'editerModele' || $cmd == "exCreerGrille" || $cmd == "rqCreerGrille")
	{	
		claro_redirect('PORTFOL/edition.php?modele_id='.$_REQUEST['modele_id']);
	}
	
	// Suppression d'un modele
	if ($cmd == 'supprimerModele')
	{	
		$modele->load($_REQUEST['modele_id']);
		if($modele->delete())
		$dialogBox .= get_lang('Template deleted');
	}
}




include get_path('incRepositorySys').'/claro_init_header.inc.php';

// Fonctions javascript
$htmlHeadXtra[] = '<script type="text/javascript" src="lib/jquery.js"></script>';
$htmlHeadXtra[] ='<script type="text/javascript" src="lib/portfolio.js"></script>';
$htmlHeadXtra[] =
"<script>
function confirmation (name)
{
    if (confirm(\" ".clean_str_for_javascript(get_lang('Are you sure to delete'))." \"+ name + \" ?\"))
        {return true;}
    else
        {return false;}
}
</script>";

$titleElement['mainTitle'] = get_lang("Portfolio");

echo claro_html_tool_title($titleElement,false);


// Gestion des modeles
if($is_allowedToEdit)
{
	echo  "<a class='claroCmd' href=\"".$_SERVER['PHP_SELF']."?cmd=rqCreerModele\">"
	     ."<img src=\"".$imgRepositoryWeb."plus.gif\" alt=\"\"> "
	            .get_lang('Create new template')
	            ."</a>\n";
}

if ($dialogBox)
{
	echo claro_html_message_box($dialogBox);
}



/*
	DISPLAY THE FULL TABLE
*/

 
echo "<table class=\"claroTable emphaseLine\" width=\"100%\">\n"
		."<tr class=\"headerX\">\n"
			."<th>".get_lang('Name')."</th>\n";
				
		if ($is_allowedToEdit)			
		{
			echo "<th>".get_lang('Creator')."</th>\n";
			echo "<th>".get_lang('Delete')."</th>\n";
			echo "<th>".get_lang('Edit')."</th>\n";
			echo "<th>".get_lang('Users')."</th>\n";

			if ($afficherStats == true)
			echo "<th>".get_lang('Statistics')."</th>\n";
		}

	echo "</tr>\n"
		 ."<tbody>";


foreach ($modele->chercherListeModele() as $ceModele)
{

	$modele_en_cours = new Modele();
	$modele_en_cours->load($ceModele['fiche_id']);

	
	if (!$is_allowedToEdit && !autoriserUtilisateur($_uid))			
	{
		continue;
	}


	echo "<tr>"
			 ."<td><a href=\"evaluation.php?modele_id=".$modele_en_cours->getModeleId()."\">".$modele_en_cours->getNom()."</a></td>";
			 
	if ($is_allowedToEdit)			
	{
		$infoCreateur = user_get_properties($modele_en_cours->getCreateur());
		echo 	"<td>".$infoCreateur['firstname']." ".$infoCreateur['lastname']."</td>";

		    if ($modele_en_cours->getCreateur() == $_uid || claro_is_platform_admin())
		   {
					echo 	"<td>",
							"<a href=\"",$_SERVER['PHP_SELF'],"?cmd=supprimerModele&modele_id=",$modele_en_cours->getModeleId(),"\" ",
							"onClick=\"return confirm('".clean_str_for_javascript(get_lang('Are you sure to delete'))." ".$modele_en_cours->getNom()." ?')\">",
							"<img src=\"".$imgRepositoryWeb."delete.gif\" border=\"0\" alt=\"".get_lang('Delete')."\">",
							"</a>",
							"</td>\n";
					
					echo "<td>"
						."<a href=\"".$_SERVER['PHP_SELF']."?cmd=editerModele&modele_id=".$modele_en_cours->getModeleId()."\">"
						."<img src=\"".$imgRepositoryWeb."edit.gif\" border=\"0\" alt=\"".get_lang('Modify')."\">"
						."</a>"
						."</td>\n";
		   }
		   else
		   echo "<td>&nbsp</td><td>&nbsp</td>";
						
				echo '<td align="center">'
				.	 '<a href="inscription.php?modele_id='.$modele_en_cours->getModeleId().'">'
				.	 '<img src="'.$clarolineRepositoryWeb.'img/group.gif" border="0" alt="'.get_lang('Details').'" />'
				.	 '</a>'
				.	 '</td>' . "\n";

				if ($afficherStats == true)
				echo "<td>"
				."<a href=\"statistiques.php?modele_id=".$modele_en_cours->getModeleId()."\">"
				."<img src=\"".$imgRepositoryWeb."statistics.gif\" border=\"0\" alt=\"".get_lang('Statistics')."\">"
				."</a>"
				."</td>\n";
	}	
	echo "</tr>";	 
}
	echo "</tbody></table>";
	
include get_path('incRepositorySys').'/claro_init_footer.inc.php';
?>
